package com.jbh.netlibrary.m;

import android.content.Context;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Handler;
import android.os.Message;

import androidx.annotation.NonNull;

import com.jbh.netlibrary.u.InitConfig;
import com.jbh.netlibrary.i.Callback;
import com.jbh.netlibrary.u.NetStateUtils;

/**
 * @author on 2023/9/27
 */
public class YxjWifiManager {

    static YxjWifiManager yxjWifiManager;

    private YxjWifiManager() {
    }

    public static YxjWifiManager newInstance() {
        if (yxjWifiManager == null) {
            synchronized (YxjWifiManager.class) {
                if (yxjWifiManager == null) {
                    yxjWifiManager = new YxjWifiManager();
                }
            }
        }
        return yxjWifiManager;
    }


    InitConfig config;

    public void setConfig(InitConfig config) {
        if (this.config == null) {
            this.config = config;
        }
    }


    /**
     * 是否是WiFi
     *
     * @param context
     * @return
     */
    public boolean isWifi(Context context) {
        int type = NetStateUtils.getNetworkType(context);
        return (type == 1);
    }


    /**
     * wifi 强弱
     *
     * @return
     */
    public WifiStrongMode wifiStrong(Context context) {
        WifiStrongMode state = WifiStrongMode.none;
        if (isWifi(context)) {
            WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
            WifiInfo info = wifiManager.getConnectionInfo();
            if (info != null) {
                //如果网络连接给个默认值
                state = WifiStrongMode.feeble;
                /*信号*/
                int wifiLever = info.getRssi();
                //信号最强
                if (wifiLever >= -50 && wifiLever < 0) {
                    state = WifiStrongMode.strong;
                }
                //较强
                else if (wifiLever >= -70 && wifiLever < -50) {
                    state = WifiStrongMode.stronger;
                }
                //较弱
                else if (wifiLever >= -80 && wifiLever < -70) {
                    state = WifiStrongMode.weak;
                }
                //微弱
                else if (wifiLever > -100 && wifiLever < 80) {
                    state = WifiStrongMode.feeble;
                }
            }
        }
        return state;
    }


    /**
     * 网络强弱
     */
    public enum WifiStrongMode {
        none, strong, stronger, weak, feeble
    }


    private Context context;
    private Callback callback;
    private long delayMillis;

    /****
     * 自动检测
     * @param context
     * @param delayMillis
     */
    public void auto(Context context, long delayMillis, Callback<WifiStrongMode> callback) {
        this.context = context;
        this.callback = callback;
        this.delayMillis = delayMillis;
        handler.removeMessages(0);
        if (isWifi(context)) {
            handler.sendEmptyMessageDelayed(0, delayMillis);
        }
    }


    private Handler handler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if (context != null) {
                YxjWifiManager.WifiStrongMode s = YxjWifiManager.newInstance().wifiStrong(context);
                callback.onState(s);
            }
            handler.sendEmptyMessageDelayed(0, delayMillis);
        }
    };
}
